# -*- coding: utf-8 -*-
"""
Created on %(date)s

@author: %(wang)s
"""
import os
import math
os.listdir(os.getcwd())
#print(os.getcwd())
#os.chdir("D:\\Users\\WANGWEIW23\\.spyder-py3")
#test1=open("wangwei1_det_0")
#test2=open("wangwei2_det_0")
###根据得到的pts(csv)文件并且将68个特征点坐标提取出来
def coordinate(test):
        a=[]
        for line in test:
            a.append(line)                  
        for i in range(len(a)):
            a[i]=a[i].split();
        for i in range(3,71):
            for j in range(2):
                a[i][j]=(float)(a[i][j])
        return a


####计算各个特征点对应的中点坐标
def computer_mean_point(b):
    midpoint=[0,0]
    for j in range(len(b)):
        midpoint[0]=midpoint[0]+b[j][0]
        midpoint[1]=midpoint[1]+b[j][1]
    midpoint[0]=midpoint[0]/len(b)
    midpoint[1]=midpoint[1]/len(b)
    return midpoint


####根据多边形的顶点的横纵坐标边界直观判断点是否在多边形内部
def left_face_forming(p0,p1,p2,p3,p4,p5):
    result=0
    xmin=min(p1[0],p2[0],p3[0],p4[0],p5[0]);
    xmax=max(p1[0],p2[0],p3[0],p4[0],p5[0]);
    ymin=min(p1[1],p2[1],p3[1],p4[1],p5[1]);
    ymax=max(p1[1],p2[1],p3[1],p4[1],p5[1]);
    if (p0[0]<xmin)or(p0[0]>xmax)or(p0[1]<ymin)or(p0[1]>ymax):
        result=0
    return result

def poly_forming(midpoint):
    length=len(midpoint)
    abc=[1]*length
    for i in range(length):
        abc[i]={'lat':midpoint[i][0],'lng':midpoint[i][1]}
    return abc

#def area_point(nvert,poly):
#    xrray=[]
#    yrray=[]
#    resultlist=[]
#    for i in range(nvert):
#        xrray.append(poly[i]['lat'])
#    for j in range(nvert):
#        yrray.append(poly[j]['lng'])
#    xmin=min(xrray)
#    xmax=max(xrray)
#    ymin=min(yrray)
#    ymax=max(yrray)
#    for x in range(math.ceil(xmin),math.ceil(xmax)):
#        for y in range(math.ceil(ymin),math.ceil(ymax)):
#            pt={'lat':x,'lng':y}
#            if isInsidePolygon(pt,poly):
#               resultlist.append([x,y]);
#    return resultlist
#                                
#def isInsidePolygon(pt, poly):
#    c = False
#    i = -1
#    l = len(poly)
#    j = l - 1
#    while i < l-1:
#        i += 1
#        print(i,poly[i], j,poly[j])
#        if ((poly[i]["lat"] <= pt["lat"] and pt["lat"] < poly[j]["lat"]) or (poly[j]["lat"] <= pt["lat"] and pt["lat"] < poly[i]["lat"])):
#            if (pt["lng"] < (poly[j]["lng"] - poly[i]["lng"]) * (pt["lat"] - poly[i]["lat"]) / (poly[j]["lat"] - poly[i]["lat"]) + poly[i]["lng"]):
#                c = not c
#        j = i
#    return c
 
###备选的判断点是否在区域内部

####根据两个点求解两点的直线方程  
def line_equation(x,y):
    a=x[1]-y[1]
    b=y[0]-x[0]
    c=x[0]*y[1]-y[0]*x[1]
    return a,b,c

###判断顶点判断某个点是否在一个多变形的内部
def isInsidePolygon(pt,nvert,poly):
    equation=[]
    for i in range(nvert-1):
        equation.append([line_equation(poly[i],poly[i+1])])
    equation.append([line_equation(poly[nvert-1],poly[0])])
    midpoint1=[(poly[0][0]+poly[1][0])/2,(poly[0][1]+poly[1][1])/2]
    midpoint2=[(poly[1][0]+poly[2][0])/2,(poly[1][1]+poly[2][1])/2]
    for j in range(1,nvert,1):
        if equation[j][0][0]*midpoint1[0]+equation[j][0][1]*midpoint1[1]+equation[j][0][2]>=0:
           equation[j].append('true')
        else:
           equation[j].append('False')
            
    if equation[0][0][0]*midpoint2[0]+equation[0][0][1]*midpoint2[1]+equation[0][0][2]>=0:
       equation[0].append('True')
    else:
       equation[0].append('False')
    for k in range(nvert):
        if (pt[0]*equation[k][0][0]+pt[1]*equation[k][0][1]+equation[k][0][2]>=0)==True:
            c=True
        if (pt[0]*equation[k][0][0]+pt[1]*equation[k][0][1]+equation[k][0][2]<=0)==False:
            c=True
        else:
            c=False
    return c

####列举出多边形区域内部的点形成矩阵区域
def area_point(nvert,poly):
    xrray=[]
    yrray=[]
    resultlist=[]
    for i in range(nvert):
        xrray.append(poly[i][0])
    for j in range(nvert):
        yrray.append(poly[j][1])
    xmin=min(xrray)
    xmax=max(xrray)
    ymin=min(yrray)
    ymax=max(yrray)
    for x in range(math.ceil(xmin),math.ceil(xmax)):
        for y in range(math.ceil(ymin),math.ceil(ymax)):
            pt=[x,y]
            if isInsidePolygon(pt,nvert,poly):
               resultlist.append([x,y]);
    return resultlist  
                    
        
    
    

    
    
        


    